#include "StdAfx.h"
#include "Door.h"


#define OPEN_DOOR   (ENT_STATES[ WALKING ])
#define CLOSE_DOOR  (ENT_STATES[ IDLE ])
#define LOCK_DOOR   (ENT_STATES[ ATTACKING1 ])

Door::Door(void)
{
    CloseDoor();
}

Door::~Door(void)
{
}


/** Update the entity */
void Door::Update( long dt)
{
    m_FSM.Update();
}

/** Collide with another entity */
bool Door::CollideWith( IEntity* ent )
{
    if ( CanOpen( ent ) )
    {
        OpenDoor();
    } 
    else
    {
        CloseDoor();
    }
    return false;
}


/** Test to see if this ent can open the door */
bool Door::CanOpen(IEntity* ent)
{
    if ( IsLocked() && IsClosed() )
    {
        if ( ent->m_attributes.Has( "keys" ) )
        {
            int keys = ent->m_attributes.GetInt( "keys" );
            if ( keys )  { 
                ent->m_attributes.SetInt( "keys", --keys ); 
                UnLockDoor();
                return true; 
            }
        }
        return false;
    }
    return true;
}

/** Open the door */
void Door::OpenDoor()
{
    SetState( OPEN_DOOR );
    m_flags |= EF_NOCOLLIDE;    // ents can walk over this
}

/** Test is this door is open */
bool Door::IsOpen()
{
    return GetState() == OPEN_DOOR;
}

/** Close the door */
void Door::CloseDoor()
{
    SetState( CLOSE_DOOR );
    m_flags &= ~EF_NOCOLLIDE;    // ents can walk over this
    
}

/** Test is this door is open */
bool Door::IsClosed()
{
    return !IsOpen();
}

/** Lock the door */
void Door::LockDoor() 
{ 
    m_locked = true; 
    SetState( LOCK_DOOR );
}